#include"binTree.h"
node_t* nodeNew(int typeN, double value, node_t* left, node_t* right)
{
    node_t* n = (node_t*) calloc(1, sizeof(node_t));

    if(n == NULL) return NULL;

    n-> value = value;
    n->typeN = NUMBER;
    n->l = left;
    n->r = right;
    return n;
}
node_t* nodeNew(int typeN, node_t* left, node_t* right)
{
    node_t* n = (node_t*) calloc(1, sizeof(node_t));

    if(n == NULL) return NULL;

    n->typeN = typeN;
    n->l = left;
    n->r = right;
    return n;
}
int printTree(const node_t* root, FILE* f)
{
    assert(f); assert(root);
    if(root->l) printTree(root->l, f);
    if(root->r) printTree(root->r, f);
    switch(root->typeN)
    {
        case NUMBER:
            fprintf(f, "push %lg\n", root->value);
            break;
        case PLUS:
            fprintf(f, "add\n");
            break;
        case MUL:
            fprintf(f, "mul\n");
            break;
        case SUB:
            fprintf(f, "sub\n");
            break;
        case DIV:
            fprintf(f, "div\n");
            break;
        default:
            fprintf(f, "SYNTAX ERROR: <%d> - unknown node type.");
            break;
    }
    return 0;
}

int tree_dtor(node_t* root)
{
    assert(root);
    if (root->l) tree_dtor(root->l);
    if (root->r) tree_dtor(root->r);
    free(root);
}
